<?php
class mg_image extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('mmg_image');
		$this->load->model('mset_menu');
	}
	
	function index()
	{
		$config['base_url'] = base_url().'mg_image/index/';
		$config['total_rows'] = $this->db->count_all('mgt_image');
		$config['per_page'] = 10;
		$config['uri_segment'] = 3;

		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';

		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page">';
		$config['first_tag_close'] = '</li>';

		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page">';
		$config['last_tag_close'] = '</li>';

		$config['next_link'] = 'Next &rarr;';

		$config['next_tag_open'] = '<li class="next page">';
		$config['next_tag_close'] = '</li>';
		
		$config['prev_link'] = '&larr; Previous';

		$config['prev_tag_open'] = '<li class="prev page">';
		$config['prev_tag_close'] = '</li>';
		
		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';

		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';

		$this->pagination->initialize($config);
	
		$token = $this->session->userdata('token');
		if($token){
			$offset=($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
			$data['results'] = $this->mmg_image->getList($config['per_page'],$offset);
			$this->load->view('mg_image/list',$data);
		}else{
			redirect('admin');
		}
	}
    
	function add(){
		$data['mode'] = "Create";
		$data['action'] = "save";
		$data['set'] = $this->mset_menu->getListGalery();
		$this->load->view('mg_image/form',$data);
	}
	
	function edit($id_menu){
		$data['mode'] = "Edit";
		$data['action'] = "update";
		$data['detail'] = $this->mmg_image->detail($id_menu);
		$data['set'] = $this->mset_menu->getListGalery();
		$this->load->view('mg_image/form',$data);
	}
    
	function save()
	{
		$id_image = $this->input->post('id_image');
		$id_menu = $this->input->post('id_menu');
		$jenis_image = $this->input->post('jenis_image');
		$deskripsi_image = $this->input->post('deskripsi_image');
		
		$config['upload_path'] = './upload/picture/';
		$config['allowed_types'] = 'jpg|png|gif';
		
		if($jenis_image == 1){
			$config['max_width']  = '1024';
			$config['max_height']  = '150';
		}elseif($jenis_image == 2){
			$config['max_width']  = '960';
			$config['max_height']  = '300';
		}elseif($jenis_image == 3){
			$config['max_width']  = '1024';
			$config['max_height']  = '800';
		}
		
		$this->load->library('upload', $config);
		$this->upload->do_upload('upload_image');
		$getFile = $this->upload->data();
		$path_foto = $getFile['file_name'];
		
		if($path_foto != ""){	
			if ($this->upload->display_errors())
			{
				$this->session->set_flashdata('error', true);
				$upload_image="";
			}
			else{
				$upload_image = $getFile['file_name'];
			}
		}else{
			$upload_image = $this->input->post('upload_image_tmp');
		}
		
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			$this->mmg_image->setData($id_image,$id_menu,$jenis_image,$upload_image,$deskripsi_image);
			$this->mmg_image->create();
			$this->session->set_flashdata('success', true);
			redirect('mg_image');
		}
	}
	
	function update()
	{
		$id_image = $this->input->post('id_image');
		$id_menu = $this->input->post('id_menu');
		$jenis_image = $this->input->post('jenis_image');
		$deskripsi_image = $this->input->post('deskripsi_image');
		
		$config['upload_path'] = './upload/picture/';
		$config['allowed_types'] = 'jpg|png|gif';
		
		if($jenis_image == 1){
			$config['max_width']  = '1024';
			$config['max_height']  = '150';
		}elseif($jenis_image == 2){
			$config['max_width']  = '960';
			$config['max_height']  = '300';
		}elseif($jenis_image == 3){
			$config['max_width']  = '1024';
			$config['max_height']  = '800';
		}
		
		$this->load->library('upload', $config);
		$this->upload->do_upload('upload_image');
		$getFile = $this->upload->data();
		$path_foto = $getFile['file_name'];
		
		if($path_foto != ""){	
			if ($this->upload->display_errors())
			{
				$this->session->set_flashdata('error', true);
				$upload_image="";
			}
			else{
				$upload_image = $getFile['file_name'];
			}
		}else{
			$upload_image = $this->input->post('upload_image_tmp');
		}
		
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			$this->mmg_image->setData($id_image,$id_menu,$jenis_image,$upload_image,$deskripsi_image);
			$this->mmg_image->update($id_image);
			$this->session->set_flashdata('success', true);
			redirect('mg_image');
		}
	}
	
	function deleteImage($id_image)
	{
		$getHapus=$this->mmg_image->detail($id_image);
		$Hapus = $getHapus[0]['upload_image'];
		@unlink("image/picture/".$Hapus);
		
		$upload_image = "";
		$this->mmg_image->setDataUpload($id_image,$upload_image);
		if ($this->mmg_image->updateImage($id_image)){
			$this->session->set_flashdata('deleteImage', true);
			redirect('mg_image','refresh');
		}	
	}	
	
	function delete($id_image)
	{
		$getHapus=$this->mmg_image->detail($id_image);
		$Hapus = $getHapus[0]['upload_image'];
		@unlink("image/picture/".$Hapus);
		if ($this->mmg_image->remove($id_image)){
			$this->session->set_flashdata('delete', true);
			redirect('mg_image','refresh');
		}	
	}	
}
?>